/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author HoangLiem
 */
public class DataTableModel extends AbstractTableModel {

    private ArrayList<String> columnNames;
    private ArrayList<ArrayList<Object>> data;
    private ArrayList<Object> longValues;

    public ArrayList<String> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(ArrayList<String> columnNames) {
        this.columnNames = columnNames;
    }

    public ArrayList<ArrayList<Object>> getData() {
        return data;
    }

    public void setData(ArrayList<ArrayList<Object>> data) {
        this.data = data;
    }

    public ArrayList<Object> getLongValues() {
        return longValues;
    }

    public void setLongValues(ArrayList<Object> longValues) {
        this.longValues = longValues;
    }

    public ArrayList<Object> initNewRow(int col) {
        ArrayList<Object> row = new ArrayList<Object>();
        if (col > 0) {
            for (int i = 0; i < col; i++) {
                row.add(new String(""));
            }
        }
        return row;
    }

    public ArrayList<Object> initLongValues(int col) {
        ArrayList<Object> longVal = new ArrayList<Object>();
        if (col > 0) {
            for (int i = 0; i < col; i++) {
                longVal.add(new String(""));
            }
        }
        return longVal;
    }

    public DataTableModel() {
    }

    public DataTableModel(ArrayList<String> columnNames, ArrayList<ArrayList<Object>> data) {
        this.columnNames = columnNames;
        this.data = data;
        if (columnNames != null && !columnNames.isEmpty()) {
            int col = columnNames.size();
            longValues = initLongValues(col);
        }
    }

    @Override
    public int getColumnCount() {
        return columnNames.size();
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames.get(col);
    }

    @Override
    public Object getValueAt(int row, int col) {
        return data.get(row).get(col);
    }

    /*
     * Định nghĩa cách hiển thị lên table
     */
    @Override
    public Class getColumnClass(int c) {
        if (getValueAt(0, c) == null) {
            return String.class;
        }
        return getValueAt(0, c).getClass();
    }

    /*
     * Cho phép chỉnh sửa trên cell hay không
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        return true;
    }

    /*
     * set gia tri tai một cell trong table
     */
    public void setValue(Object value, int row, int col) {
        ArrayList<Object> oldRow = data.get(row);
        oldRow.add(col, value);
        oldRow.remove(col + 1);
        data.add(row, oldRow);
        data.remove(row + 1);
    }
    /*
     * Lấy tất cả field
     */

    public ArrayList<String> getAllFieldName() {
        ArrayList<String> fieldNameList = new ArrayList<String>();
        if (data != null && !data.isEmpty()) {
            for (int i = 0; i < data.size(); i++) {
                String fieldName = (String) data.get(i).get(0);
                if (!fieldName.equals("")) {
                    fieldNameList.add(fieldName);
                }
            }
        } else {
            fieldNameList = null;
        }
        return fieldNameList;
    }

    /*
     * 
     */
    @Override
    public void setValueAt(Object value, int row, int col) {
        setValue(value, row, col);
        fireTableCellUpdated(row, col);
    }
}
